
/**
 * 路径函数
 */

/**
 * 去除url中指定参数
 * @param paramKey
 * @returns {string}
 */
export function delUrl参数 (url,paramKey){
		if(url.indexOf('?')<=-1){
			return url;
		}
        var urlarr = url.split("?");
        var urlParam = urlarr[1];   //页面参数
        var beforeUrl = urlarr[0];   //页面主地址（参数之前地址）
        var nextUrl = "";
        var arr = new Array();
        if (urlParam != "") {
            var urlParamArr = urlParam.split("&"); //将参数按照&符分成数组
            for (var i = 0; i < urlParamArr.length; i++) {
                var paramArr = urlParamArr[i].split("="); //将参数键，值拆开
                //如果键雨要删除的不一致，则加入到参数中
                if (paramArr[0] != paramKey) {
                    arr.push(urlParamArr[i]);
                }
            }
        }
        if (arr.length > 0) {
            nextUrl = "?" + arr.join("&");
        }
        url = beforeUrl + nextUrl;
        return url;
}
// 增加当前url中某个参数
export function  addUrl参数 (url,paramKey,paramKeyVal){
	var arr = new Array();
	var beforeUrl = url;
	if(url.indexOf('?')>-1){
		var urlarr = url.split("?");
		var urlParam = urlarr[1];   //页面参数
		beforeUrl = urlarr[0];   //页面主地址（参数之前地址）
		var nextUrl = "";
		if (urlParam != "") {
			var urlParamArr = urlParam.split("&"); //将参数按照&符分成数组
			for (var i = 0; i < urlParamArr.length; i++) {
				var paramArr = urlParamArr[i].split("="); //将参数键，值拆开
				//如果键雨要删除的不一致，则加入到参数中
				if (paramArr[0] != paramKey) {
					arr.push(urlParamArr[i]);
				}
			}
		}
	}
	if(paramKeyVal){
		arr.push(paramKey+"="+paramKeyVal);
	}
	if (arr.length > 0) {
		nextUrl = "?" + arr.join("&");
	}
	url = beforeUrl + nextUrl;
	return url;
}

//替换内容中的img的src
export function  replaceimgSrc (domainURL,htmlstr) {
  let regex = new RegExp(/src=['"]([^'"]+)/gi);
  let htmlstr1 = htmlstr.replace(regex, function (match, capture) {
	console.log("capture:", capture);
	if(!my.isUrl(capture)){
		capture = domainURL + capture;
		var newStr = "src='" + capture+"' style='max-width:100%';display:block;";
		return newStr;
	}else{
		var newStr = "src='" + capture+"' style='max-width:100%;display:block;'";
		return newStr;
	}
  });
  regex = null;
  return htmlstr1;
}
//跳转页面
export function redirectUrl(url){
	if(!url || url == ""){
		return false;
	}
	var old_url = url;
	url = url.split("?")
	url = url[0];
	if(my.isUrl(url)){
		// #ifdef APP-PLUS
		plus.runtime.openURL(url);
		// #endif
		// #ifdef H5
		location.href=url
		// #endif
	}else if(url != "/pages/index/index" && url != "/pages/shop/index" && url != "/pages/wallet/index" && url != "/pages/user/index"){
		uni.navigateTo({
			url:old_url
		})
	}else{
		uni.switchTab({
			url:old_url
		})
	}
}

//获取当前路由和参数
export function getPath参数  (){
	let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
	let curRoute = routes[routes.length - 1].route //获取当前页面路由
	/* let curParam = routes[routes.length - 1].options; //获取路由参数
	// 拼接参数
	let param = ''
	for (let key in curParam) {
	    param += '&' + key + '=' + curParam[key]
	}
	return curRoute+(param?"?"+param:"");*/
	return curRoute;
}

//获取url参数
export function getUrl参数 (variable){
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}
